﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Chronos.Presentation
{
    public class DelegateCommand : ICommand
    {
        public delegate void SimpleCommandHandler(object sender, SimpleCommandEventArgs e);
        private SimpleCommandHandler _handler;
        private bool _isEnabled = true;

        public event EventHandler CanExecuteChanged;

        public DelegateCommand(SimpleCommandHandler handler)
        {
            _handler = handler;
        }

        public bool CanExecute(object parameter)
        {
            return _isEnabled;
        }

        public void Execute(object parameter)
        {
            _handler(this, new SimpleCommandEventArgs(parameter));
        }

        public bool IsEnabled
        {
            get { return _isEnabled; }
            set 
            {
                _isEnabled = value;
                OnCanExecuteChanged();
            }

        }

        protected virtual void OnCanExecuteChanged()
        {
            EventHandler local = CanExecuteChanged;
            if (local != null)
                local(this, EventArgs.Empty);
        }
    }
}
